/*  PrimeBlaster
    by Kjetil Eide 
    kjetile445@gmail.com

    This file is part of PrimeBlaster

    PrimeBlaster is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    PrimeBlaster is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrimeBlaster.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.eide.spaceship;

import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.FastTrig;

public class StarField {

	/* the higher the slower */
	private static final int STAR_SPEED = 10;
	private float STAR_ROTATION_WIDTH = 2000;
	
	/* the z-coordinate of the stars that are the farthest away
	 * if this is changed, colors will be wrong
	 */	
	private static final int MAX_Z = 1024;

	private float starX[];
	private float starY[];
	private float starZ[];
	private int minX;
	private int maxX;
	private int minY;
	private int maxY;
	private int numberOfStars;
	private float angle;
	
	private float angleOffset;
	
	//private Rectangle starShape;
    //private Image starImage;
	
	private Random rnd;
	
	public StarField(int numberOfStars, int width, int height) throws SlickException {
		//starImage = new Image("spacedata/star.tga");
		//starShape = new Rectangle(0, 0, 1, 1);
		rnd = new Random();
		this.numberOfStars = numberOfStars;
		starX = new float[numberOfStars];
		starY = new float[numberOfStars];
		starZ = new float[numberOfStars];
		minX = -width/2;
		maxX = width/2;
		minY = -height/2;
		maxY = height/2;
		angle = 0;
		
		for  (int i = 0; i < numberOfStars; i++) {
			makeStar(i);
		}			
	}
	
	public void updateStars(int delta) {
		float zOffset = delta / STAR_SPEED;
		
		for (int i = 0; i < numberOfStars; i++) {
			starZ[i] -= zOffset;
			if (starZ[i] < 0 ||
					starX[i] < minX || starX[i] > maxX ||
					starY[i] < minY || starY[i] > maxY) {
				makeStar(i, MAX_Z);
			} 
		}
		angleOffset += delta/STAR_ROTATION_WIDTH;	
		angle = angle + (float) FastTrig.sin(angleOffset);
	}
	
	private void makeStar(int idx) {
		makeStar(idx, rnd.nextFloat() * MAX_Z);
	}
	
	private void makeStar(int idx, float Z) {
		starX[idx] = rnd.nextFloat() * maxX - 200;
		starY[idx] = rnd.nextFloat() * maxY - 200;
		starZ[idx] = Z;
	}
	
	public void drawStars(Graphics g) {
		for (int i = 0; i < numberOfStars; i++) {
			float x = (256 * starX[i] / starZ[i]) + maxX; 
			float y = (256 * starY[i] / starZ[i]) + maxY; 
			int red = 0;
			int green = 0;
			int blue = 255;
				 
			if (starZ[i] > 0.75 * MAX_Z) {
				blue = MAX_Z - (int) starZ[i];
			} else {
				red = 256 - (int) starZ[i] / 3;
				green = red;
			}
	
			g.rotate(maxX, maxY, angle);
			g.setColor(new Color(red, green, blue));
			g.drawRect(x, y, 1, 1);
			g.resetTransform();
		}
	}
}
	